package com.fzshang.shopping.wx.controller;


import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fzshang.shopping.commen.bean.User;
import com.fzshang.shopping.commen.bean.UserInfo;
import com.fzshang.shopping.commen.controller.BaseController;
import com.fzshang.shopping.commen.entity.ApiResult;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 * 后台用户 前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2020-03-03
 */
@Controller
@RequestMapping("wx/userInfo")
public class WXUserInfoController extends BaseController {


    /**
     * @param userInfo
     * @return 商家入驻
     */
    @RequestMapping(value = "save")
    @ResponseBody
    public ApiResult save(@RequestBody UserInfo userInfo){
        userInfo.setState(1);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("openid",userInfo.getOpenid());
        UserInfo userInfo1 = userInfoService.getOne(queryWrapper);
        if(userInfo1!=null) {
            return ApiResult.fail("您已提交入驻,后台会在24小时之内短信通知您结果!请等候...");
        }
        //查询是否呦账号重复
        List<UserInfo> userInfoList = userInfoService.list();
        for (int i = 0; i < userInfoList.size(); i++) {
            if(userInfoList.get(i).getAccount().equals(userInfo.getAccount())){
                return ApiResult.fail("已存在该账户!");
            }
        }
        userInfo.setCreateTime(System.currentTimeMillis());
        //使用密码给MD5加密
        String md5Hex1 = DigestUtil.md5Hex(userInfo.getPassWord());
        userInfo.setPassWord(md5Hex1);
        if(userInfoService.save(userInfo)){
            QueryWrapper queryWrapper1 = new QueryWrapper();
            queryWrapper1.eq("openid",userInfo.getOpenid());
            User user = userService.getOne(queryWrapper1);
            user.setUserIdentity(2);
            user.setTel(userInfo.getPhone());
            userService.updateById(user);
            return ApiResult.ok();
        }
        return ApiResult.fail();
    }

}
